﻿using System;
using System.Data;
using System.Windows.Forms;

namespace Stote.Forms
{
    public partial class FrmStoreAddAndEdit : BaseForm
    {
        Dong.BLL.Store storeBLL = new Dong.BLL.Store();
        Dong.Model.Store editModel = null;
        int editId = 0;
        public FrmStoreAddAndEdit(int id, Form parent)
        {
            InitializeComponent();

            btnAdd.Click += BtnAdd_Click;

            if (id == 0)
            {
                editId = 0;
                editModel = null;
                WinText = "添加";
                btnAdd.Text = "添加";
                btnAdd.Image = Properties.Resources.add;
            }
            else
            {
                editId = id;
                editModel = storeBLL.GetModel(editId);
                WinText = "编辑";
                btnAdd.Text = "保存";
                btnAdd.Image = Properties.Resources.save;

                txtStoreName.Text = editModel.StoreName;
                txtStorePlace.Text = editModel.StorePlace;
                rtbRemark.Text = editModel.Remark;
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtStoreName.Text.Trim()))
                {
                    MessageBox.Show("请输入仓库名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }


                if (editId == 0)
                {
                    DataSet ds = storeBLL.GetList($" Status=0 AND StoreName='{txtStoreName.Text.Trim()}'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show($"此仓库名称{txtStoreName.Text.Trim()}已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    Dong.Model.Store model = new Dong.Model.Store()
                    {
                        StoreName = txtStoreName.Text.Trim(),
                        StorePlace = txtStorePlace.Text.Trim(),
                        Remark = rtbRemark.Text.Trim(),
                        CreateUserId = FrmLogin.CurrentUser.UserId,
                    };

                    storeBLL.Add(model);
                }
                else
                {
                    DataSet ds = storeBLL.GetList($" Status=0 AND StoreName='{txtStoreName.Text.Trim()}' AND StoreId!={editModel.StoreId}");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show($"修改后的仓库名称{txtStoreName.Text.Trim()}已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    editModel.StoreName = txtStoreName.Text.Trim();
                    editModel.StorePlace = txtStorePlace.Text.Trim();
                    editModel.Remark = rtbRemark.Text.Trim();
                    editModel.LastUpdateUserId = FrmLogin.CurrentUser.UserId;
                    editModel.LastUpdateTime = DateTime.Now;
                    storeBLL.Update(editModel);
                }
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }
}
